//
//  loginView.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/7.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "loginView.h"

@interface loginView ()

@property (nonatomic, strong) UIView *pwdLine;
@property (nonatomic, strong) UIView *accountLine;
@property (nonatomic, strong) QMUITextField *accountField;
@property (nonatomic, strong) QMUITextField *pwdField;
@property (nonatomic, strong) UIButton *resettingPwdBtn; // 忘记密码
@property (nonatomic, strong) UIButton *goToRegisterBtn; // 去注册
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *skipBtn;
@property (nonatomic, strong) UIImageView *logoImage;


@end

@implementation loginView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        _accountLine = [UIView new];
        _pwdLine     = [UIView new];
        _accountLine.backgroundColor = [UIColor grayColor];
        _pwdLine.backgroundColor     = _accountLine.backgroundColor;
        _accountField = [QMUITextField new];
        _pwdField     = [QMUITextField new];
        _accountField.font = UIFontMake(14);
        _pwdField.font     = _accountField.font;
        _accountField.textColor = [UIColor whiteColor];
        _pwdField.textColor     = _accountField.textColor;
        _accountField.placeholderColor = JS_colorWithHex(0xaaaaaa);             // >>设置占位文字颜色
        _pwdField.placeholderColor     = _accountField.placeholderColor;
        _accountField.placeholder = @"用户名";
        _pwdField.placeholder     = @"密码";
        _resettingPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goToRegisterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _resettingPwdBtn.titleLabel.font = UIFontMake(13);
        _goToRegisterBtn.titleLabel.font = _resettingPwdBtn.titleLabel.font;
        [_resettingPwdBtn setTitleColor:JS_colorWithHex(0xffffff) forState:UIControlStateNormal];
        [_goToRegisterBtn setTitleColor:JS_colorWithHex(0xffffff) forState:UIControlStateNormal];
        [_resettingPwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_goToRegisterBtn setTitle:@"去注册" forState:UIControlStateNormal];
        [_resettingPwdBtn sizeToFit];
        [_goToRegisterBtn sizeToFit];
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 70, 200, 40)];
        [_loginBtn addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.titleLabel.font = UIFontMake(14);
        [_loginBtn setTitleColor:JS_colorWithHex(0xffffff) forState:UIControlStateNormal];
        _loginBtn.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
        _loginBtn.layer.cornerRadius = 5;
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _skipBtn.titleLabel.font = UIFontMake(14);
        [_skipBtn setTitleColor:JS_colorWithHex(0xffffff) forState:UIControlStateNormal];
        _skipBtn.backgroundColor = [UIColor clearColor];
        [_skipBtn setTitle:@"继续浏览" forState:UIControlStateNormal];
        [_skipBtn sizeToFit];
        _logoImage = [UIImageView new];
        _logoImage.image = UIImageMake(@"logo");
        [_logoImage sizeToFit];
        
        [self js_addSubviews:@[_accountLine,_pwdLine,_accountField,_pwdField,_resettingPwdBtn,_goToRegisterBtn,_loginBtn,_skipBtn,_logoImage]];
        [self.pwdLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(kRealValue(12));
            make.right.equalTo(self).offset(kRealValue(-12));
            make.height.mas_equalTo(1);
            make.centerY.equalTo(self);
        }];
        [self.pwdField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.pwdLine.mas_top);
            make.left.and.right.equalTo(self.pwdLine);
            make.height.mas_equalTo(kRealValue(48));
        }];
        [self.accountLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.pwdField.mas_top);
            make.left.and.right.equalTo(self.pwdLine);
            make.height.equalTo(self.pwdLine);
        }];
        [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self.pwdLine);
            make.height.equalTo(self.pwdField);
            make.bottom.equalTo(self.accountLine.mas_top);
        }];
        [self.resettingPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pwdLine).offset(kRealValue(8));
            make.top.equalTo(self.pwdLine.mas_bottom).offset(kRealValue(10));
        }];
        [self.goToRegisterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.pwdLine).offset(kRealValue(-8));
            make.top.equalTo(self.resettingPwdBtn);
        }];
        [self.goToRegisterBtn layoutIfNeeded];
        NSLog(@"%@",NSStringFromCGRect(self.goToRegisterBtn.frame));
        self.loginBtn.frame = CGRectMake(kRealValue(12), self.goToRegisterBtn.js_y + self.goToRegisterBtn.height + kRealValue(6), SCREEN_WIDTH - kRealValue(24), kRealValue(48));
//        [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.and.right.equalTo(self.pwdLine);
//            make.height.equalTo(self.pwdField);
//            make.top.equalTo(self.resettingPwdBtn.mas_bottom).offset(kRealValue(6));
//        }];
        [self.skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.loginBtn.mas_bottom).offset(kRealValue(12));
            make.centerX.equalTo(self.loginBtn);
        }];
        [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.accountField.mas_top).offset(kRealValue(-30));
        }];
        // 设置left view
        UIImageView *accountLeftView = [UIImageView new];
        
        accountLeftView.image = UIImageMake(@"icon_telephone");
        UIImageView *pwdLeftView = [UIImageView new];
        pwdLeftView.image = UIImageMake(@"icon_lock");
        [accountLeftView sizeToFit];
        [pwdLeftView sizeToFit];
        _accountField.leftView = accountLeftView;
        _pwdField.leftView     = pwdLeftView;
        _accountField.leftViewMode = UITextFieldViewModeAlways;
        _pwdField.leftViewMode     = UITextFieldViewModeAlways;
        [_pwdField setSecureTextEntry:YES];
    }
    return self;
}
-(void)loginClick:(UIButton *)btn{
    if (btn.selected) {
        btn.selected = NO;
        [self.loginBtn js_endLoadingAniamtion];

    }else{
        btn.selected = YES;
        [self.loginBtn js_startLoadingAniamtion];

    }
}
@end
